<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="服务名称：">
            <el-input v-model="listQuery.courseName" class="input-width" placeholder="输入名称" clearable></el-input>
          </el-form-item>
          <!--          <el-form-item label="来源机构：">-->
          <!--            <el-input v-model="listQuery.deptName" class="input-width" placeholder="输入名称" clearable></el-input>-->
          <!--            &lt;!&ndash;<el-select&ndash;&gt;-->
          <!--            &lt;!&ndash;v-model="listQuery.deptId"&ndash;&gt;-->
          <!--            &lt;!&ndash;filterable&ndash;&gt;-->
          <!--            &lt;!&ndash;remote&ndash;&gt;-->
          <!--            &lt;!&ndash;reserve-keyword&ndash;&gt;-->
          <!--            &lt;!&ndash;clearable&ndash;&gt;-->
          <!--            &lt;!&ndash;placeholder="请输入关键词"&ndash;&gt;-->
          <!--            &lt;!&ndash;:remote-method="remoteMethod"&ndash;&gt;-->
          <!--            &lt;!&ndash;:loading="loading">&ndash;&gt;-->
          <!--            &lt;!&ndash;<el-option&ndash;&gt;-->
          <!--            &lt;!&ndash;v-for="item in deptOptions"&ndash;&gt;-->
          <!--            &lt;!&ndash;:key="item.value"&ndash;&gt;-->
          <!--            &lt;!&ndash;:label="item.label"&ndash;&gt;-->
          <!--            &lt;!&ndash;:value="item.value">&ndash;&gt;-->
          <!--            &lt;!&ndash;</el-option>&ndash;&gt;-->
          <!--            &lt;!&ndash;</el-select>&ndash;&gt;-->
          <!--          </el-form-item>-->
          <el-form-item label="分类：">
            <el-cascader
              v-model="listQuery.courseType"
              :options="courseTypeOptions"
              clearable
              :props="{
                children:'subset',
                label: 'typeName',
                value: 'id'
              }">
            </el-cascader>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()">新增服务</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border>
        <el-table-column label="服务名称" align="center">
          <template slot-scope="scope">{{ scope.row.courseName }}</template>
        </el-table-column>
        <!--        <el-table-column label="类型" align="center">-->
        <!--          <template slot-scope="scope">{{ scope.row.typeName }}</template>-->
        <!--        </el-table-column>-->
        <el-table-column label="分类" align="center">
          <template slot-scope="scope">{{ scope.row.deptName }}</template>
        </el-table-column>
        <el-table-column label="更新时间" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.updateTime | formatDateTime }}</template>
        </el-table-column>
        <!--        <el-table-column label="更新频率" align="center">-->
        <!--          <template slot-scope="scope">{{ scope.row.frequency }}</template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="视频数量" align="center">-->
        <!--          <template slot-scope="scope">{{ scope.row.videoCount }}</template>-->
        <!--        </el-table-column>-->
        <el-table-column label="操作" width="240" align="center">
          <template slot-scope="scope">
            <!--            <el-button-->
            <!--              size="mini"-->
            <!--              type="text"-->
            <!--              @click="handleVideoList(scope.$index, scope.row)">课程视频-->
            <!--            </el-button>-->
            <el-button
              size="mini"
              type="text"
              @click="handleReview(scope.$index, scope.row)">查看
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { curCourseBaseList, curCourseTypeList, curCourseBaseDelById, umsDeptList } from '@/api/course'
import { formatDate } from '@/utils/date'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
}
export default {
  name: 'CourseList',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      typeOptions: Object.assign({}),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      operates: [
        {
          label: '删除',
          value: 0
        }
      ],
      operateType: null,
      courseTypeOptions: [],
      loading: false,
      deptOptions: []
    }
  },
  created () {
    this.getCourseType()
    this.getList()
  },
  filters: {
    formatDateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete (index, row) {
      this.deleteCouerse(row.id)
    },
    handleVideoList (index, row) {
      this.$router.push({ path: '/course-ms/videoList', query: { id: row.id } })
    },
    handleReview (index, row) {
      this.$router.push({ path: '/serv-ms/reviewServ', query: { id: row.id } })
    },
    handleAdd () {
      this.$router.push({ path: '/serv-ms/addServ' })
    },
    handleUpdate (index, row) {
      this.$router.push({ path: '/serv-ms/updateServ', query: { id: row.id } })
    },
    getList () {
      this.listLoading = true
      const { courseType, deptId } = this.listQuery
      const params = {
        ...this.listQuery
      }
      if (courseType && courseType.length > 0) {
        params.courseType = courseType[courseType.length - 1]
      }
      if (deptId) {
        params.deptName = deptId
      }
      curCourseBaseList(params).then(response => {
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
      })
    },
    deleteCouerse (ids) {
      this.$confirm('是否要删除该课程?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = new URLSearchParams()
        params.append('id', ids)
        curCourseBaseDelById(params).then(response => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    getCourseType () {
      this.courseTypeOptions = []
      // 获取课程类型枚举
      curCourseTypeList().then(response => {
        this.courseTypeOptions = response.data.list
      })
    },
    remoteMethod (query) {
      if (query !== '') {
        this.loading = true
        umsDeptList({ pageSize: 50, pageNum: 1 }).then(response => {
          this.loading = false
          this.deptOptions = response.data.list.map(item => {
            return { value: `${item.id}`, label: `${item.deptName}` }
          })
        })
      } else {
        this.deptOptions = []
      }
    }
  }
}
</script>
<style scoped>
  .input-width {
    width: 203px;
  }
</style>
